package com.ruoyi.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.domain.TLaboratory;
import com.ruoyi.admin.domain.vo.RepairVo;
import com.ruoyi.admin.domain.vo.TLaboratoryVo;
import com.ruoyi.admin.service.ITLaboratoryService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.admin.domain.Repair;
import com.ruoyi.admin.service.IRepairService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验室维修Controller
 * 
 * @author lin
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/lab/repair")
public class RepairController extends BaseController
{
    @Autowired
    private IRepairService repairService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITLaboratoryService itLaboratoryService;

    /**
     * 查询实验室维修列表
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(Repair repair)
    {
        startPage();
        List<RepairVo> list = repairService.selectRepairList(repair);
        return getDataTable(list);
    }

    /**
     * 导出实验室维修列表
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:export')")
    @Log(title = "实验室维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Repair repair)
    {
        List<RepairVo> list = repairService.selectRepairList(repair);
        ExcelUtil<RepairVo> util = new ExcelUtil<RepairVo>(RepairVo.class);
        util.exportExcel(response, list, "实验室维修数据");
    }

    /**
     * 获取实验室维修详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:query')")
    @GetMapping(value = "/{repairId}")
    public AjaxResult getInfo(@PathVariable("repairId") Long repairId)
    {
        return success(repairService.selectRepairByRepairId(repairId));
    }

    /**
     * 新增实验室维修
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:add')")
    @Log(title = "实验室维修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Repair repair)
    {
        TLaboratoryVo tLaboratoryVo = itLaboratoryService.selectTLaboratoryByLabId(repair.getLabId());
        if (tLaboratoryVo==null){
            return error("新增实验室维修信息失败");
        }
        return toAjax(repairService.insertRepair(repair));
    }

    /**
     * 修改实验室维修
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:edit')")
    @Log(title = "实验室维修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Repair repair)
    {
        SysUser sysUser = sysUserService.selectUserById(repair.getTeacherId());
        if (sysUser == null){
            return error("新增实验室维修信息失败");
        }
        TLaboratoryVo tLaboratoryVo = itLaboratoryService.selectTLaboratoryByLabId(repair.getLabId());
        if (tLaboratoryVo==null){
            return error("新增实验室维修信息失败");
        }
        return toAjax(repairService.updateRepair(repair));
    }

    /**
     * 删除实验室维修
     */
    @PreAuthorize("@ss.hasPermi('lab:repair:remove')")
    @Log(title = "实验室维修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairIds}")
    public AjaxResult remove(@PathVariable Long[] repairIds)
    {
        return toAjax(repairService.deleteRepairByRepairIds(repairIds));
    }
}
